<?php
error_reporting(0);
	include_once 'config.php';
	include_once './Charge/tools.php';
	$last_day = (int)date("d");
if(isset($_GET['action']) && $_GET['action'] == 'yes'){
	if(!$_POST['key'] == null && !$_POST['url'] == null){
		$key = $_POST['key'];
		$url = $_POST['url'];
		$resource=mysql_connect($host.":".$port,$username,$password);
		if (mysql_select_db($db,$resource)){
			if($resource){
				$sql = "SELECT * FROM `CDKey` WHERE `api_Key` = '$key'";
				$result=mysql_query($sql,$resource);
				if(mysql_num_rows($result) == 0){
					echo '<script>alert("Key不存在,或Key已被使用！");</script>';
					echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
				}
				else if(mysql_num_rows($result) == 1){
					$LongUrl = get_curl("http://dwz.cn/query.php","tinyurl=".$_POST['url']);
					$select_url_result = strpos($LongUrl,"www.battleofballs.com");
					if($select_url_result == false){
						echo '<script>alert("未查询到该链接的id\n若有问题请联系站长");</script>';
						echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
					}
					else{
						$id = getSubstr($LongUrl,"?id=","&Account");
						$row = mysql_fetch_assoc($result);
						$Day = $row['Day'];
						
						$add_select_sql="SELECT * FROM `charge` WHERE `id` = '$id'";
						$add_select_result=mysql_query($add_select_sql,$resource);
						if(mysql_num_rows($add_select_result) == 1){
							$add_row_1 = mysql_fetch_assoc($add_select_result);
							$day_1 = $add_row_1['DueTime'];
							$day_2 = null;
							if((int)$day_1 < 0 || (int)$day_1 == 0){
								$day_2 = (int)$Day;
							}
							else{
								$day_2 = (int)$day_1 +(int)$Day;
							}
							$add_sql_1="UPDATE `charge` SET `CDkey` = '$key', `DueTime` = '$day_2', `overdue` = 'no',`url` = '$url' WHERE `charge`.`id` = '$id'"; 
							$add_result=mysql_query($add_sql_1,$resource);
							if($add_result){
								$del_key_sql="DELETE FROM `CDKey` WHERE `CDKey`.`api_Key` = '$key'";
								$result_del_key=mysql_query($del_key_sql,$resource);
								if($result_del_key == true){
									echo '<script>alert("续费成功！当前剩余天数：'.$day_2.'\n若有问题请联系站长");</script>';
									echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
								}
								else{
									echo '<script>alert("续费成功,但最后部署未完成,为了保护您的利益,请与站长联系!\n若有问题请联系站长");</script>';
									echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
								}
							}
							else{
								echo '<script>alert("续费失败，请重试，若频繁出现该问题，请联系站长。\n若有问题请联系站长\n");</script>';
								echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
							}
						}
						else if(mysql_num_rows($add_select_result) == 0){
							$add_sql="INSERT INTO `charge` (`id`, `CDkey`, `DueTime`, `overdue`,`url`) VALUES ('$id', '$key', '$Day', 'no','$url')"; 
							$add_result_1=mysql_query($add_sql,$resource);
							if($add_result_1 == true){
								$del_key_sql="DELETE FROM `CDKey` WHERE `CDKey`.`api_Key` = '$key'";
								$result_del_key=mysql_query($del_key_sql,$resource);
								if($result_del_key == true){
									echo '<script>alert("开通成功！当前剩余天数：'.$Day.'\n若有问题请联系站长");</script>';
									echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
								}
								else{
									echo '<script>alert("开通成功,但最后部署未完成,为了保护您的利益,请与站长联系!\n若有问题请联系站长");</script>';
									echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
								}
							}
							else{
								echo '<script>alert("开通失败，请重试，若频繁出现该问题，请联系站长。\n若有问题请联系站长");</script>';
								echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
							}
						}
						else{
							echo '<script>alert("检测到异常，请联系站长解决！\n若有问题请联系站长");</script>';
							echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
						}
					}
				}
				mysql_close($resource);
			}
		}
		else{
			echo '<script>alert("数据库链接失败，请刷新本页重试！\n若有问题请联系站长");</script>';
			echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
		}
	}
	else if(!$_POST['key'] == null && $_POST['url'] == null){
		$key = addslashes($_POST['key']);
		
		$resource=mysql_connect($host.":".$port,$username,$password);
		if (mysql_select_db($db,$resource)){
			if($resource){
				$sql="SELECT * FROM `charge` WHERE `CDkey` = '$key'";
				$result=mysql_query($sql,$resource);
				if($result == false){
					echo '<script>alert("抱歉！查询失败,请重试！若频繁出现此问题,请联系站长！\n若有问题请联系站长");</script>';
					echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
				}
				else{
					if(mysql_num_rows($result) == 0){
						echo '<script>alert("抱歉！没有查询到记录~\n若有问题请联系站长");</script>';
						echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
					}
					else if(mysql_num_rows($result) == 1){
						$row = mysql_fetch_assoc($result);
						if($row['overdue'] == 'yes'){
							$due = '已到期';
						}
						else if($row['overdue'] == 'stop'){
							$due = '我们极力维护狼族网成为一个对用户公平公正的平台<br />但由于种种原因，我们不得不对该ID停止服务<br />如果您是该ID的主人，请及时与我们联系解决问题。<br />为了用户的权益，我们一直在努力:)';
						}
						else{
							$due = $row['DueTime']."天";
						}
						if($row['url'] == null){
							$uurl_user = '暂无记录';
						}
						else{
							$uurl_url = $row['url'];
							$result_uurl = bdUrlAPI(0,$uurl_url);
							$select_uurl = strpos($result_uurl,"www.battleofballs.com");
							if($select_uurl == false){
								$uurl_user = '暂无记录';
							}
							else{
								$result_uurl .= "'";
								$uurl_user = urldecode(getSubstr($result_uurl,"Account=","'"));
							}
						}
						if(isset($row['last_day'])){
							if((int)$row['last_day'] == $last_day){
								$server = "<b style='color: green;'>今日已完成</b>";
							}
							else{
								$server = "<b style='color: orange;'>正在任务队列中</b>";
							}
						}
						else{
							$server = "<b style='color: red;'>未知</b>";
						}
						echo '<center>';
						echo "<h3>用户名</h3>";
						echo "<b style='color: red;'>".$uurl_user."</b>";
						echo '<br />';
						echo '<h3>您的链接</h3>';
						echo "<b style='color: red;'>".$row['url']."</b>";
						echo '<br />';
						echo "<h3>使用的CDKey</h3>";
						echo "<b style='color: red;'>".$row['CDkey']."</b>";
						echo "<h3>剩余服务天数</h3>";
						echo "<b style='color: red;'>".$due."</b>";
						echo "<h3>服务器状态</h3>";
						echo $server;
						echo "<br />";
						echo "<input type='submit' onclick='javascript:window.location.href = ".'"'."$index_url".'"'.";' value='返回' />";
						echo '<center>';
					}
					else{
						echo '<script>alert("数据好像出现异常了哦~！所以服务器拒绝了您的查询~\n若有问题请联系站长");</script>';
						echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
					}
				}	
				mysql_close($resource);
			}
		}
		else{
			echo '<script>alert("数据库链接失败，请刷新本页重试！\n若有问题请联系站长");</script>';
			echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
		}
	}
	else if($_POST['key'] == null && !$_POST['url'] == null){
		$url = addslashes($_POST['url']);
		
		$LongUrl = get_curl("http://dwz.cn/query.php","tinyurl=".$_POST['url']);
		$json_result = json_decode($LongUrl,true);
			$select_url_result = strpos($json_result['longurl'],"www.battleofballs.com");
			if($select_url_result == false){
				echo '<script>alert("未查询到该链接的id\n若有问题请联系站长");</script>';
				echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
			}
			else{
				$id = getSubstr($LongUrl,"?id=","&Account");
			}
		$resource=mysql_connect($host.":".$port,$username,$password);
		if (mysql_select_db($db,$resource)){
			if($resource){
				$sql="SELECT * FROM `charge` WHERE `id` = '$id'";
				$result=mysql_query($sql,$resource);
				if($result == false){
					echo '<script>alert("抱歉！查询失败,请重试！若频繁出现此问题,请联系站长！\n若有问题请联系站长");</script>';
					echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
				}
				else{
					if(mysql_num_rows($result) == 0){
						echo '<script>alert("抱歉！没有查询到记录~\n若有问题请联系站长");</script>';
						echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
					}
					else if(mysql_num_rows($result) == 1){
						$row = mysql_fetch_assoc($result);
						if($row['overdue'] == 'yes'){
							$due = '已到期';
						}
						else if($row['overdue'] == 'stop'){
							$due = '我们极力维护狼族网成为一个对用户公平公正的平台<br />但由于种种原因，我们不得不对该ID停止服务<br />如果您是该ID的主人，请及时与我们联系解决问题。<br />为了用户的权益，我们一直在努力:)';
						}
						else{
							$due = $row['DueTime']."天";
						}
						if($row['url'] == null){
							$uurl_url = $url;
							$result_uurl = bdUrlAPI(0,$uurl_url);
							$select_uurl = strpos($result_uurl,"www.battleofballs.com");
							if($select_uurl == false){
								$uurl_user = '暂无记录';
							}
							else{
								$result_uurl .= "'";
								$uurl_user = urldecode(getSubstr($result_uurl,"Account=","'"));
							}
						}
						else{
							$uurl_url = $row['url'];
							$result_uurl = bdUrlAPI(0,$uurl_url);
							$select_uurl = strpos($result_uurl,"www.battleofballs.com");
							if($select_uurl == false){
								$uurl_user = '暂无记录';
							}
							else{
								$result_uurl .= "'";
								$uurl_user = urldecode(getSubstr($result_uurl,"Account=","'"));
							}
						}
						if(isset($row['last_day'])){
							if((int)$row['last_day'] == $last_day){
								$server = "<b style='color: green;'>今日已完成</b>";
							}
							else{
								$server = "<b style='color: orange;'>正在任务队列中</b>";
							}
						}
						else{
							$server = "<b style='color: red;'>未知</b>";
						}
						echo '<center>';
						echo "<h3>用户名</h3>";
						echo "<b style='color: red;'>".$uurl_user."</b>";
						echo '<br />';
						echo '<h3>您的链接</h3>';
						echo "<b style='color: red;'>".$url."</b>";
						echo '<br />';
						echo "<h3>使用的CDKey</h3>";
						echo "<b style='color: red;'>".$row['CDkey']."</b>";
						echo "<h3>剩余服务天数</h3>";
						echo "<b style='color: red;'>".$due."</b>";
						echo "<h3>服务器状态</h3>";
						echo $server;
						echo "<br />";
						echo "<input type='submit' onclick='javascript:window.location.href = ".'"'."$index_url".'"'.";' value='返回' />";
						echo '<center>';
					}
					else{
						echo '<script>alert("数据好像出现异常了哦~！所以服务器拒绝了您的查询~\n若有问题请联系站长");</script>';
						echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
					}
				}	
				mysql_close($resource);
			}
		}
	}
	else{
		echo '<script>alert("不能为空！");</script>';
		echo "<script>window.location.href = '$index_url"."/charge.php"."';</script>";
	}
}
else{
	echo '<center>';
	echo '<span style="font-size: 15px; color: red;">↓↓ 查询时填写Key或链接任意一个即可 ↓↓</span>';
	echo '<form action="charge.php?action=yes" method="post">';
	echo '<input type="text" class="bd1 xi20 huang2 cen" name="key" placeholder="请输入Key" />';
	echo '<input type="text" class="bd1 xi20 huang2 cen" name="url" placeholder="请输入链接" />';
	echo '<input type="submit" value="提交/查询" />';
	echo '</form>';
	echo '<input type="submit" id="Pay" value="购买Key" />';
	echo '<br />';
	echo '<div style="text-align:left;" id="Help">';
	echo '<b style="color: red;">使用说明：</b><br />购买卡密后，将卡密填入第一个输入框内，然后将链接输入第二个输入框内，点击提交，显示成功即可，如需查询，请先使用Key后再查询，查询时只填写Key即可<br />';
	echo '<span style="font-family: 微软雅黑; font-size: 20px;"><b style="color: red;">购买须知：</b><br />';
	echo '1.一个KEY对应一个链接<br />';
	echo '2.遇到球球更新，可能会中断服务<br />';
	echo '3.KEY购买渠道一律在网站公布，可直接进入<br />';
	echo '4.每天凌晨0:00-4:00完成所有链接<br />';
	echo '5.最好保留您购买的KEY(每个链接最后一次使用过的),方便日后查询/查证<br />';
	echo '6.当天提交的链接，次日会到账</span>';
	echo '</div>';
	echo '<br />';
	echo '</center>';
}
?>